<?php
namespace MyAdmin\Model;

use Think\Model;

class UsersModel extends Model
{
  protected $autoCheckFields =false;

  public function set_user_session($info)
  {
    $_SESSION["user_id"] = $info['id'];
    $_SESSION["phone"] = $info['phone'];
    $_SESSION["token"] = $info['token'];
    $_SESSION["nickname"] = $info['nickname'];
  }

  //退出
  public function logout_users()
  {
    session_unset();
    session_destroy();
    setcookie('uautoadmin', '', (gmtime()-C('AUTO_LOGIN_TIME')*3600*24), C('cookie_pe'));
  }

  //获取信息
  public function get_userinfo($user_id)
  {
    $info = M("Users")->where("status=1 and id=".$user_id)->find();
    return $info;
  }

  //创建
  public function create_token($user_id)
  {
    $now_time = gmtime();
    $i=1;
    while ($i<2) {
      $token=random(28);
      $result=M("users")->where("token='".$token."'")->getField("id");
      if (!$result) {
        if ($user_id) {
          $update_data['token']=$token;
          $update_data['expire_time']=$now_time+7*3600*24;//暂时不利用过期时间==先记录7天
          M("users")->where("id='".$user_id."'")->save($update_data);
        }

        $i=2;
      }
    }

    return $token;
  }
}
